package com.lh.ssm.web;

import com.github.pagehelper.PageInfo;
import com.lh.ssm.domain.Product;
import com.lh.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 展示某类商品信息
     */
    @GetMapping("/productshow")
    public String show(int tid, HttpServletRequest request,Integer currentPage) {
        if(currentPage == null){
            currentPage = 1;
        }

        PageInfo<Product> pageInfo = productService.findProducts(tid,currentPage);


        HashMap<String, Object> map = new HashMap<>();
        map.put("id",tid);
        map.put("currentPage",currentPage);
        map.put("totalPage",pageInfo.getPages());
        map.put("list",pageInfo.getList());
        request.setAttribute("pageBean",map);

        return "goodsList";
    }

    /**
     * 根据商品id,查询对应的商品详情
     */
    @GetMapping("/productdetail")
    public String detail(String pid, HttpServletRequest request) {

        //根据id查询商品详情
        Product product = productService.findProductById(pid);

        //将结果传输到前端
        request.setAttribute("product",product);
        return "goodsDetail";
    }
}
